﻿// -------------------------------------------------------------------------
//    @FileName         :    NFRegisterCache.cpp
//    @Author           :    gaoyi
//    @Date             :    24-8-15
//    @Email            :    445267987@qq.com
//    @Module           :    NFRegisterCache
//
// -------------------------------------------------------------------------

#include "NFRegisterCache.h"

#include <NFLogicConfig.h>

#include "proto_common_trade.nanopb.h"

NFRegisterCache::NFRegisterCache()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFRegisterCache::~NFRegisterCache()
{
}

int NFRegisterCache::CreateInit()
{
    m_iPreRegistNum = 0;
    return 0;
}

int NFRegisterCache::ResumeInit()
{
    return 0;
}

int NFRegisterCache::AfterAllConnectAndAllDescStore(NF_SERVER_TYPE serverType)
{
    if (serverType != NF_ST_LOGIC_SERVER) return 0;

    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetRunMode())
    {
        Load(NF_ST_LOGIC_SERVER);
    }
    return 0;
}

int NFRegisterCache::AddQQRegisteNum()
{
    m_stData.qq_registe_num++;

    MarkDirty();

    return 0;
}

int NFRegisterCache::AddWXRegisteNum()
{
    m_stData.wx_registe_num++;

    MarkDirty();

    return 0;
}

int NFRegisterCache::AddPreRegistNum()
{
    ++m_iPreRegistNum;

    LOGSVR_TRACE("add pre-registnum! PreRegistNum:" << m_iPreRegistNum);

    return 0;
}

int NFRegisterCache::DelPreRegistNum()
{
    --m_iPreRegistNum;

    LOGSVR_TRACE("del pre-registnum! PreRegistNum:" << m_iPreRegistNum);

    m_iPreRegistNum = std::max(0, m_iPreRegistNum);

    return 0;
}

bool NFRegisterCache::CheckRegNumFull()
{
    if (NFLogicConfig::Instance()->IsForbidRegist())
    {
        return true;
    }

    if (NFLogicConfig::Instance()->IsRegistLimit())
    {
        if (NFLogicConfig::Instance()->GetPlatID() == 1)
        {
            if (GetQQRegisteNum() + m_iPreRegistNum < NFLogicConfig::Instance()->GetQQRegistLimit())
            {
                return false;
            }
        }
        else //if(CServerUtil::GameSvrConfig()->GetPlatID() == 2 )
        {
            if (GetWXRegisteNum() + m_iPreRegistNum < NFLogicConfig::Instance()->GetWXRegistLimit())
            {
                return false;
            }
        }
    }
    else
        return false;

    return true;
}

int NFRegisterCache::LoadFromDB(const proto_ff::tbMiscData &dbData)
{
    m_stData.FromPb(dbData.data().account_registe_info());
    return 0;
}

int NFRegisterCache::SaveToDB(proto_ff::tbMiscData &dbData)
{
    m_stData.ToPb(dbData.mutable_data()->mutable_account_registe_info());
    return 0;
}

int NFRegisterCache::InitConfig()
{
    MarkDirty();
    return 0;
}

int NFRegisterCache::GetDbId()
{
    return EN_MISC_ID_REGISTER_CONTROL;
}
